package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
 @Autowired
 private SetmealDishService setmealDishService;
 @Autowired
 private DishService dishService;

 @Override
 public R<String> save1(SetmealDto setmealDto) {
  this.save(setmealDto);
  Long id = setmealDto.getId();
  List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
  setmealDishes.stream().forEach(
   (item) -> {
    item.setSetmealId(id);
   }
  );
  setmealDishService.saveBatch(setmealDishes);
  return R.success("保存成功");
 }

 public Page<SetmealDto> page(Integer page, Integer pageSize, String name) {
  SetmealDto setmealDto = new SetmealDto();
  Page<SetmealDto> pageInfo = new Page<>(page, pageSize);
  Page<SetmealDto> setmealDtoPage = this.getBaseMapper().pageByParams(pageInfo, setmealDto);
  return setmealDtoPage;
 }

 @Override
 public R<String> deleteByIds(List<Long> longs) {
  for (Long aLong : longs) {
   Setmeal setmeal = this.getById(aLong);
   if (setmeal.getStatus() == 0) {
    this.removeById(aLong);
   } else {
    throw new CustomException("起售状态菜品无法删除");
   }
  }
  return R.success("删除成功");
 }

 @Override
 public R<String> updateStatus(Integer status, List<Long> ids) {
  LambdaQueryWrapper<Setmeal> dqw = new LambdaQueryWrapper<>();
  dqw.in(Setmeal::getId, ids);
  List<Setmeal> list = this.list(dqw);
  for (Setmeal setmeal : list) {
   setmeal.setStatus(status);
  }
  this.updateBatchById(list);
  return R.success("修改成功");
 }

 @Override
 public R<List<Setmeal>> getDish(Setmeal setmeal) {
  //select *from dish where dish.catgory_id=setmeal.catgory_id;
  LambdaQueryWrapper<Setmeal> dqw = Wrappers.<Setmeal>lambdaQuery().
   eq(Setmeal::getCategoryId, setmeal.getCategoryId());
  List<Setmeal> list = this.list(dqw);
  return R.success(list);
 }

 @Override
 public SetmealDto getByIdWithDishes(Long setmealId) {
  Setmeal setmeal1 = this.getById(setmealId);
  SetmealDto setmealDto = new SetmealDto();
  BeanUtils.copyProperties(setmeal1, setmealDto);
  //查询当前套餐里面含有的菜品信息，从setmeal_dishe表查询
  LambdaQueryWrapper<SetmealDish> dqw = Wrappers.<SetmealDish>lambdaQuery()
   .eq(SetmealDish::getSetmealId, setmealId);
  List<SetmealDish> setmealDishes = setmealDishService.list(dqw);
  setmealDto.setSetmealDishes(setmealDishes);
  return setmealDto;
 }

 @Override
 public SetmealDto upDateWithSetmealDishes(SetmealDto setmealDto) {
  //更新setmeal表的信息
  this.updateById(setmealDto);
  //清理当前套餐对应的菜品---setmeal_dis表的delete操作
  LambdaQueryWrapper<SetmealDish> dqw = Wrappers.<SetmealDish>lambdaQuery()
   .eq(SetmealDish::getSetmealId, setmealDto.getId());
  setmealDishService.remove(dqw);
  //添加当前提交过来的菜品信息--setmeal_dish表的insert操作
  List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
  setmealDishes=setmealDishes.stream().map(item -> {
   item.setSetmealId(setmealDto.getId());
   return item;
  }).collect(Collectors.toList());
  setmealDishService.saveBatch(setmealDishes);
  return setmealDto;
 }
}
